package com.liuyucheng.tcpudp.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author LiuYuCheng
 * @date 2023年06月06日 22:43
 */
public class ReceiveDemo {
    public static void main(String[] args) throws IOException {
        //创建Socket接收端对象，并指明端口号为10086，与发送端保持一致
        DatagramSocket d = new DatagramSocket(10086);
        while (true) {
            //使用字节数组接收数据
            byte[] bytes = new byte[1024];
            //
            DatagramPacket p = new DatagramPacket(bytes, bytes.length);
            //接收数据包
            d.receive(p);
            System.out.println("发送方信息：" + p.getAddress().getHostAddress());
            //解析数据
            System.out.println("数据是：" + new String(p.getData(), 0, p.getLength()));
        }
    }
}